import java.io.*;
import java.util.*;

public class GenerateHtmlJavaScript {

    private static RandomAccessFile outputFile;
   
    private static String workDir    = "C:/myProjects/apache2/htdocs/";
    private static String serverUrl  = "/";
    private static String currentDir = null;
    private static String baseDir    = null; 
    private static String systemDir  = null; 
    private static String htmlDir    = null; 
    private static String jspDir     = null; 
   
    private static Hashtable profileHsTable = null; //new Hashtable ();

    public void makeHtmlFrame( )
    {

	//GenerateHtmlJavaScript maker = new GenerateHtmlJavaScript();

	createLeftBarHtmlFile(baseDir);

	if ((new File ((baseDir+"/_system"), "profile.txt")).exists()) {
	    System.out.println("Found profile for;"+baseDir);
	    loadLeftBarProfile();
	    System.out.println("end of loading profile for;"+baseDir);
	}

	addHtmlHeadToLeftBar();
	generateJavaScriptFile(jspDir, "sideBarStateBody.js");

	addJavaScriptFileToLeftBar(("/_system/javaScript"+"/"), "setCookie.js");
	addJavaScriptFileToLeftBar(("/_system/javaScript"+"/"), "sideBarStateHead.js");
	//addJavaScriptFileToLeftBar(("./_system/javaScript"+"/"), "csideBarStateBody.js");
	addJavaScriptFileToLeftBar(("./_system/javaScript"+"/"), "sideBarStateBody.js");
	addJavaScriptFileToLeftBar(("/_system/javaScript"+"/"), "sideBarStateEnd.js");
	addJavaScriptFileToLeftBar(("/_system/javaScript"+"/"), "sideBarDisplay.js");

	addHtmlEnd();
	closeFile(outputFile);
    }

 
    private void createLeftBarHtmlFile(String html_dir)
    {

	File file_name = new File (html_dir, "index.html");

	System.out.println("createLeftBarHtmlFile:"+html_dir);
	try {
	    if (file_name.exists()) {
		System.out.println("deleting old index_left.html file .....");
		if(file_name.delete()) {
		    System.out.println("Old index_left.html file has been deleted");
		}
	    }
	    outputFile = new RandomAccessFile( file_name, "rw" );

	}
	catch ( IOException e ) {
	}
    }

    private void loadLeftBarProfile() {

	SymbolFileLoader loader = new SymbolFileLoader();
	loader.setFileName(baseDir+"/_system/"+"profile.txt");
	loader.loadUrlProfile();
	profileHsTable = new Hashtable();
	profileHsTable = loader.getHashTable();
    }

    public void addHtmlHeadToLeftBar( )
    {
       try {
	   outputFile.writeBytes("<HTML><HEAD><TITLE>"+currentDir+" Side Bar</TITLE></HEAD><BODY bgcolor=\"#808080\">");
	   outputFile.writeBytes("<TABLE width=300>");
	   outputFile.writeBytes("<TR><TD><A href=/_system/html/index_left.html>3WCenter");
	   outputFile.writeBytes("</A></TD></TR><tr><td><IMG src=/_system/images/L.gif>"+
				 currentDir.toUpperCase()+"</TD></TR></TABLE><BR><BR>");

	   outputFile.writeBytes("<A href=/index/servlet/EditSideBarServlet?dir_id="+currentDir+" target=mainFrame>");
	   outputFile.writeBytes("<IMG border=0 src=/_system/images/edit.gif></A><BR><BR>");

       }
       catch ( IOException io ) {
       }
    }


    private void generateJavaScriptFile(String jsdir, String jsfile)
    {
	
	File file_name = new File (jsdir, jsfile);

	try {

	    if (file_name.exists()) {
		System.out.println("deleting old "+jsfile+" file .....");
		if(file_name.delete()) {
		    File cf = new File (jsdir, ("c"+jsfile));
		    cf.delete();
		    System.out.println("Old "+file_name.getName()+" file has been deleted");
		    System.out.println("Old "+cf.getName()+" file has been deleted");
		}
	    }
	   
	    RandomAccessFile output_js_file = new RandomAccessFile( file_name, "rw" );
	    File cf = new File (jsdir, "csideBarStateBody.js");
	    RandomAccessFile output_cjs_file = new RandomAccessFile( cf, "rw" );

	    if ((file_name.getName()).equals("setCookie.js")) {
		output_js_file.writeBytes("function setCurrState(setting) {");
		output_js_file.writeBytes("document.cookie = \"currState=\" + escape(setting);");
		output_js_file.writeBytes("currState = setting};");	
	    }

	    if ((file_name.getName()).equals("sideBarStateBody.js")) {

		/**
		 * add user profile into side bar
		 **/

		int i = 1;

		if (profileHsTable != null) {
		    String    url_name   = null;
		    String    url_adds   = null;

		    try {

			/**
			 * add profile links
			 */

			output_js_file.writeBytes("db["+ i +"]  = new dbRecord(true,\"<font size=3>");
			output_js_file.writeBytes("TOPLINKS</font>\", \"\",0,\"\",true);");
			output_cjs_file.writeBytes("cdb["+ i +"]  = new cdbRecord(true,\"<font size=3>");
			output_cjs_file.writeBytes("TOPLINKS</font>\", \"\",0,\"\",true);");
			i++;

			Enumeration enum = profileHsTable.keys();  
			while (enum.hasMoreElements()) {
			    url_name = (String)enum.nextElement();
			    url_adds = (String)profileHsTable.get(url_name);
			    if(!url_name.startsWith("#")) {
				output_js_file.writeBytes("db["+i+"]= new dbRecord(false,\"<font size=2>");
				output_js_file.writeBytes(url_name.toUpperCase()+"</font>\",");
				output_js_file.writeBytes("\""+url_adds+"\",1,\"\",true);");
				output_cjs_file.writeBytes("cdb["+i+"]= new cdbRecord(false,\"<font size=2>");
				output_cjs_file.writeBytes(url_name.toUpperCase()+"</font>\",");
				output_cjs_file.writeBytes("\""+url_adds+"\",1,\"\",true);");
				i++;
			    }
			}
		    }
		    catch ( IOException io ) {
		    }
		}
		else {
		    System.out.println("No profile for:"+baseDir);
		}

		/**
		 * add user directories into side bar
		 **/

		String   sub_dir       = null;
		String[] file_list     = null;
		String[] sub_file_list = null;

		file_list = getDirList(baseDir);

		for (int n = 0; n < file_list.length; n++) {

		    File f_file_list = new File (baseDir, file_list[n]);
	    
		    if (f_file_list.isDirectory()) {
			sub_dir = file_list[n];
			System.out.println("subDir="+sub_dir);

			if(!sub_dir.startsWith("_")) {

			    sub_file_list = getDirList(baseDir+sub_dir);

			    if (sub_file_list.length > 0) {

				/**
				 * add sub directory name into side bar
				 */

				try {
				    output_js_file.writeBytes("db["+i+"]= new dbRecord(true,\"");
				    output_js_file.writeBytes(sub_dir.toUpperCase() +"\", \"\",0,\"\",true);");
				    output_cjs_file.writeBytes("cdb["+i+"]= new cdbRecord(true,\"");
				    output_cjs_file.writeBytes(sub_dir.toUpperCase() +"\", \"\",0,\"\",true);");
				    i++;
				}
				catch ( IOException io ) {
				}

				/**
				 * add file for sub directory
				 */


				File f_sub_file_list;
				String sub_string = null;
				for(int j =0; j < sub_file_list.length; j++) {
				    
				    /**
				     * add edit link
				     **/

				    /*
				    if(j==0) { 
					try {
					    output_js_file.writeBytes("db["+i+"]= new dbRecord(false,");
					    output_js_file.writeBytes("\""+"<IMG border=0 src=/_system/images/edit.gif>"+"\",");
					    output_js_file.writeBytes("\"/"+"index"+"/servlet/"+"EditSideBarServlet?dir_id="+currentDir+
								      "&sdir_id="+sub_dir+
								      "\",1,\"\",true);");

					    i++;
					}
					catch (IOException io) {
					} 
				    }
				    */

				    f_sub_file_list = new File ((baseDir+sub_dir), sub_file_list[j]);
				
				    if (f_sub_file_list.isFile()) {

					try {
					    String temp =f_sub_file_list.getName();
					    sub_string = (temp).substring(0,temp.indexOf("."));
					    output_js_file.writeBytes("db["+i+"]= new dbRecord(false,");
					    output_js_file.writeBytes("\""+sub_string.toUpperCase()+"\",");
					    output_js_file.writeBytes("\""+serverUrl+currentDir+"/"+sub_dir+"/"+temp+"\",1,\"\",true);");
					    output_cjs_file.writeBytes("cdb["+i+"]= new cdbRecord(false,");
					    output_cjs_file.writeBytes("\""+sub_string.toUpperCase()+"\",");
					    output_cjs_file.writeBytes("\""+serverUrl+currentDir+"/"+sub_dir+"/"+temp+"\",1,\"\",true);");
					    i++;
					}
					catch ( IOException io ) {
					}
				    }
				    /*
				    else {
					try {
					    output_js_file.writeBytes("db["+i+"]= new dbRecord(true,");
					    output_js_file.writeBytes("\""+f_sub_file_list.getName() +"\", \"\",1,\"\",true);");
					    i++;
					}
					catch ( IOException io ) {
					}
				    
				    }
				    */
				}
			    }
			}
		    }
		    else {
			System.out.println("n="+file_list[n]);
		    }
		}
		/*
		
		*/
		closeFile(output_js_file);
		/*
		File cf = new File (jsdir, "csideBarStateBody.js");
		RandomAccessFile c_js_file = new RandomAccessFile( cf, "rw" );
		for (int total=1; total<i; total++) {
		    c_js_file.writeBytes("cdb["+total+"]= new cdbRecord(true);");
		}
		*/
		closeFile(output_cjs_file);
	    }
	}
	catch ( IOException e ) {
	}
    }

    private void addJavaScriptFileToLeftBar(String jsdir, String jsfile) {
	try {
	    outputFile.writeBytes("<SCRIPT SRC=\"" +jsdir+jsfile+
				  "\"></SCRIPT>");
	}
	catch ( IOException io ) {
	}
    }

    public void addHtmlEnd( )
    {
	try {
	    outputFile.writeBytes("</BODY></HTML>");
	}
	catch ( IOException io ) {
	}
    }

    private void closeFile(RandomAccessFile raf) 
    {
	try {
	    raf.close();
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

    public void setCurrentDir(String current_dir)
    {
	currentDir = current_dir;
	baseDir    = workDir + currentDir+"/";
	systemDir  = baseDir + "_system/";
	File f1 = new File (systemDir);
	f1.mkdir();
	htmlDir    = systemDir + "html/";
	File f2 = new File (htmlDir);
	f2.mkdir();
	jspDir     = systemDir + "javaScript/";
	File f3 = new File (jspDir);
	f3.mkdir();
    }

    public String getCurrentDir()
    {
	return currentDir;
    }

    public static void main( String args[] )
    {

	GenerateHtmlJavaScript maker = new GenerateHtmlJavaScript();

	if (args.length != 1) {
	    System.out.println("==============================================================");
	    System.out.println("Enter the sub_dir name of htdocs you want to generate file");
	    System.out.println("==============================================================");
	    System.exit(0);
	}
	else {
	    maker.setCurrentDir(args[0]);

	    File f_dir = new File (workDir+currentDir);
	    if (!f_dir.isDirectory()) {
		System.out.println((workDir+currentDir)+" is not a directory!");
		System.exit(0);
	    }
	}
	maker.makeHtmlFrame();
    }
}



